#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sip
sip.setapi('QString', 2)
sip.setapi('QStringList', 2)
sip.setapi('QVariant', 2)

from PyQt4 import QtCore, QtGui
from src.CollectionTreeConfigController import CollectionTreeConfigController
from src.CollectionTreeController import CollectionTreeController
from src.CollectionTreeModel import CollectionTreeModel
from src.CollectionTreeSortFilterController import \
    CollectionTreeSortFilterController
from src.PlayerConfigController import PlayerConfigController
from src.PlayerController import PlayerController
from src.PlaylistTreeController import PlaylistTreeController
from src.PlaylistTreeFilterController import PlaylistTreeFilterController
from src.PlaylistTreeModel import PlaylistTreeModel
from src.PlaylistTreeProxyModel import PlaylistTreeProxyModel
from src.Server import ServerController
from src.SongLabelController import SongLabelController
from src.ui.CollectionTreeConfigView import CollectionTreeConfigView
from src.ui.CollectionTreeSortFilterView import CollectionTreeSortFilterView
from src.ui.PlayerConfigView import PlayerConfigView
from src.ui.PlayerView import PlayerView
from src.ui.PlaylistTreeFilterView import PlaylistTreeFilterView
from src.ui.SongLabelView import SongLabelView
from src.ui.Ui_mpd_client import Ui_MainWindow
import locale
import mpd
import sys
import time

locale.setlocale(locale.LC_ALL, "")


#_______________________________________________________________________________
"""classe principale per la view"""
class MainView(QtGui.QMainWindow, Ui_MainWindow):
    def __init__(self, parent = None):
        QtGui.QMainWindow.__init__(self, parent)
        self.setupUi(self)
        self.setCentralWidget(self.pl_widget)
        self.collection_settings_frame.setVisible(False)
        self.collection_search_frame.setVisible(False)
        self.mpdclient = mpd.MPDClient()

        self.player_view = PlayerView(self)
        self.collection_sort_filter_view = CollectionTreeSortFilterView(self)
        self.collection_config_view = CollectionTreeConfigView(self)
        self.player_config_view = PlayerConfigView(self)
        
        self.server_ctrl = ServerController(self.mpdclient)
        self.player_ctrl = PlayerController(self.server_ctrl, self.player_view)
        self.songlabel_ctrl = SongLabelController(self.server_ctrl, self.songlabel_view)
        self.pl_tree_model = PlaylistTreeModel(self)
        self.pl_tree_ctrl = PlaylistTreeController(self.server_ctrl, 
                                                    self.pl_tree_model,
                                                    self.pl_tree_view)
        self.pl_tree_filter_view = PlaylistTreeFilterView(self)
        self.pl_tree_filter_proxy = PlaylistTreeProxyModel(self)
        self.pl_tree_filter_ctrl = PlaylistTreeFilterController(self.server_ctrl,
                                                                self.pl_tree_filter_proxy,
                                                                self.pl_tree_filter_view)
        self.player_config_ctrl = PlayerConfigController(self.server_ctrl,
                                                         self.player_config_view)
        self.collection_model = CollectionTreeModel(self)
        self.collection_ctrl = CollectionTreeController(self.server_ctrl,
                                                        self.collection_model,
                                                        self.collection_view)
        self.collection_sort_filter_ctrl = CollectionTreeSortFilterController(self.server_ctrl,
                                                                     self.collection_model,
                                                                     self.collection_sort_filter_view)
        self.collection_config_ctrl = CollectionTreeConfigController(self.server_ctrl,
                                                                     self.collection_model,
                                                                     self.collection_config_view)
        
        self.actionConnect.triggered.connect(self.connect)
        
    def connect(self):
        self.server_ctrl.connect()
        self.pl_tree_view.setModel(self.pl_tree_filter_proxy)
        self.pl_tree_filter_proxy.setSourceModel(self.pl_tree_model)
        self.collection_view.setModel(self.collection_model)
        self.collection_config_view.set_model(self.collection_model)
        self.collection_sort_filter_view.set_model(self.collection_model)
        
    def closeEvent(self, event):
        self.server_ctrl.disconnect()
    
#_______________________________________________________________________________
"""main"""
def main(args):
    app = QtGui.QApplication(args)
    mpdclientgui = MainView()
    mpdclientgui.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main(sys.argv)
